package com.koalac.ironman.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;

/**
 * @author
 */
public class ScreenInfoUtil {
	
	public  View mask;
	
	/** 获取屏幕信息 */
	public static DisplayMetrics getDisplayMetrics(Context context) {
		DisplayMetrics metric = new DisplayMetrics();
		((Activity) context).getWindowManager().getDefaultDisplay()
				.getMetrics(metric);
		return metric;
	}

	/**
	 * 获取手机屏幕宽度
	 * 
	 * @param context
	 * @return
	 */
	public static int getScreenWidth(Context context) {
		DisplayMetrics metric = new DisplayMetrics();
		((Activity) context).getWindowManager().getDefaultDisplay()
				.getMetrics(metric);
		return metric.widthPixels;
	}

	/**
	 * 获取手机屏幕高度
	 * 
	 * @param context
	 * @return
	 */
	public static int getScreenHeight(Context context) {
		DisplayMetrics metric = new DisplayMetrics();
		((Activity) context).getWindowManager().getDefaultDisplay()
				.getMetrics(metric);
		return metric.heightPixels;
	}

	/**
	 * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
	 */
	public static int dp2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}
	/**
	 * 给当前的手机屏幕添上一层遮罩
	 * 全屏遮罩,暂时还没找到显示局部遮罩的办法
	 */
	public  void showMask(Context context,View topView) {
		RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);
		mask.setLayoutParams(lp);
		/**
		 * 创建遮罩
		 */
		if(mask == null){
			mask = new View(context);
			int maskColor = Color.argb(136, 0, 0, 0);
			mask.setBackgroundColor(maskColor);
		}
		/**
		 * 添加遮罩
		 */
		ViewGroup groupView = (ViewGroup)((Activity)context).getWindow().getDecorView();
		groupView.addView(mask);
	}
	/**
	 * 给当前的手机屏幕移除遮罩(如果有遮罩的话)
	 */
	public  void removeMask(Context context) {
		ViewGroup groupView = (ViewGroup)((Activity)context).getWindow().getDecorView();
		if(mask != null){
			groupView.removeView(mask);
		}
	}

}
